// 选择题

const Choice = require("../../model/question/choice")
const {
    checkNull,
    isJson,
    handleNumber,

} = require("../../utils/verification")

let attributes = [
    "id",
    "title",
    "options",
    "answer",
    "creator",
    "createdate",
    "updator",
    "update",
    "chapter_id",
    "difficulty",
    "usage"
]

// 根据id获取题数据
exports.getById = async ({ id }) => {
    try {
        if (!id) return { msg: "获取失败 id is undefind", code: "400" }
        let res = await Choice.findOne({ attributes, where: { id } })
        res.options = JSON.parse(res.options)
        if (res) return { msg: "获取成功", code: "200", data: res }
        return { msg: "获取失败", code: "400" }
    } catch (err) { return { msg: "获取试题失败", code: "400", err } }
}
// 获取题列表
exports.getList = async ({ pageNum, pageSize, chapter_id }) => {
    // try {
        let checkedNum = handleNumber(pageNum, "pageNum")
        if (checkedNum.code !== "200") return checkedNum
        let checkedSize = handleNumber(pageSize, "pageSize")
        if (checkedSize.code !== "200") return checkedSize
        let res = await Choice.findAndCountAll({
            offset: checkedNum.data * checkedSize.data - checkedSize.data,
            limit: checkedSize.data,
            attributes,
            where:{chapter_id}
        })
        if (res) {
            console.log(typeof res.options);
            res.rows.forEach(item => {
                item.options = JSON.parse(item.options)
            })
            return { msg: "获取成功", code: "200", data: res }
        } else {
            return { msg: "获取失败", code: "400" }
        }
    // } catch (err) { return { msg: "获取试题失败", code: "400", err } }
}

// 添加试题
exports.create = async ({ title, chapter_id, options, answer, creator, difficulty }) => {
    try {
        let checkedTittle = checkNull(title, "选择题题干")
        if (checkedTittle.code !== "200") return checkedTittle
        let checkedType = checkNull(chapter_id, "章节id")
        if (checkedType.code !== "200") return checkedType
        let checkedAnswer = checkNull(answer, "选择题答案")
        console.log(checkedAnswer);
        if (checkedAnswer.code !== "200") return checkedAnswer
        let checkedOptions = isJson(options, "选项")
        if (checkedOptions.code !== "200") return checkedOptions
        let checkedDifficulty = checkNull(difficulty, "难易程度", 2)
        if (checkedDifficulty.code !== "200") return checkedDifficulty
        let newData = {};
        if (!creator) newData.creator = creator
        newData.difficulty = difficulty
        newData.title = title
        newData.chapter_id = chapter_id
        newData.answer = checkedAnswer.data.toUpperCase()
        if (typeof options == "string") {
            options = JSON.parse(options)
        }
        if (options.length <= 1) return { msg: "添加失败 最少包含两个选项", code: "400" }
        // 选项字母转换成大写
        options.forEach(item => {
            item.sign = item.sign.toUpperCase()
        })
        options = JSON.stringify(options)
        newData.options = options
        let res = await Choice.create(newData)
        if (res) return { msg: "添加成功", code: "200" }
        return { msg: "添加失败", code: "400" }
    } catch (err) { console.log(err); return { msg: "操作试题失败", code: "400", err } }
}

// 编辑题
exports.edit = async ({ id, title, chapter_id, options, answer, updator, difficulty }) => {
    try {
        let checkedId = checkNull(id, "试题ID")
        if (checkedId.code !== "200") return checkedId
        let newData = {};
        if (title) newData.title = title;
        if (chapter_id) newData.chapter_id = chapter_id;
        if (options) {
            if (options.length <= 1) return { msg: "添加失败 最少包含两个选项", code: "400" }
            newData.options = options;
        }
        if (answer) newData.answer = answer.toUpperCase();
        if (updator) newData.updator = updator;
        if (difficulty) {
            let checkedDifficulty = checkNull(difficulty, "难易程度", 2)
            if (checkedDifficulty.code !== "200") return checkedDifficulty
            newData.difficulty = difficulty;
        }
        let res = await Choice.update(newData, { where: { id } })
        if (res) return { msg: "编辑成功", code: "200" }
        return { msg: "编辑失败", code: "400" }
    } catch (err) { return { msg: "编辑试题失败", code: "400", err } }
}

// 删除题
exports.remove = async ({ id }) => {
    try {
        let checkedId = checkNull(id, "试题ID")
        if (checkedId.code !== "200") return checkedId;
        let res = await Choice.destroy({ where: { id } })
        if (res) return { msg: "删除成功", code: "200" }
        return { msg: "删除失败", code: "400" }
    } catch (err) { return { msg: "删除试题失败", code: "400", err } }
}